package com.dianning.web.action.user;

import java.util.Date;

import org.apache.commons.logging.*;

import com.dianning.bean.Money;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.comm.Util;
import com.dianning.service.InfoService;
import com.dianning.service.MoneyService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.action.BaseAction;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class MoneyLogAction extends BaseAction implements UserSessionAware{

	private static final Log logger = LogFactory.getLog(MoneyLogAction.class);
	
	private MoneyService moneyService;

	public MoneyService getMoneyService() {
		return moneyService;
	}

	public void setMoneyService(MoneyService moneyService) {
		this.moneyService = moneyService;
	}
	
	
	private Date startTime;
	private Date endTime;


	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	private UserSession userSession;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	private PageList pageList;
	private Money money;

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public Money getMoney() {
		return money;
	}

	public void setMoney(Money money) {
		this.money = money;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	
	public String index(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		StringBuffer sb = new StringBuffer();
		sb.append("/user/moneyLog.action");
		if(startTime != null){
			sb.append("?startTime=");
			sb.append(Util.formatDate(startTime,"yyyy-MM-dd"));
		}
		if(endTime != null){
			if(startTime == null){
				sb.append("?");
			}else{
				sb.append("&");
			}
			sb.append("endTime=");
			sb.append(Util.formatDate(endTime,"yyyy-MM-dd"));
		}
		pages.setFileName(sb.toString());
		this.setPageList(this.getMoneyService().findMoneyLogs(userSession.getUid(), this.startTime, this.endTime, "", "addTime", 1, pages));
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "index";
	}
	
	public String in(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		StringBuffer sb = new StringBuffer();
		sb.append("/user/moneyLog.action?action?action=in");
		if(startTime != null){
			sb.append("&startTime=");
			sb.append(Util.formatDate(startTime,"yyyy-MM-dd"));
		}
		if(endTime != null){
			sb.append("&endTime=");
			sb.append(Util.formatDate(endTime,"yyyy-MM-dd"));
		}
		pages.setFileName(sb.toString());
		
		this.setPageList(this.getMoneyService().findMoneyLogs(userSession.getUid(), this.startTime, this.endTime, "in", "addTime", 1, pages));
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "in";
	}
	
	public String out(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		StringBuffer sb = new StringBuffer();
		sb.append("/user/moneyLog.action?action?action=out");
		if(startTime != null){
			sb.append("&startTime=");
			sb.append(Util.formatDate(startTime,"yyyy-MM-dd"));
		}
		if(endTime != null){
			sb.append("&endTime=");
			sb.append(Util.formatDate(endTime,"yyyy-MM-dd"));
		}
		pages.setFileName(sb.toString());
		this.setPageList(this.getMoneyService().findMoneyLogs(userSession.getUid(), this.startTime, this.endTime, "out", "addTime", 1, pages));
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "out";
	}
	
}
